package com.fjh.springboot_01.mapper;

import com.fjh.springboot_01.pojo.homework.KnowledgePoint;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface KnowledgePointMapper {
    @Insert("INSERT INTO knowledge_point (name, description, category_id) " +
            "VALUES (#{name}, #{description}, #{categoryId})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(KnowledgePoint knowledgePoint);

    @Select("SELECT kp.*, c.category_name as categoryName " +
            "FROM knowledge_point kp " +
            "LEFT JOIN category c ON kp.category_id = c.id")
    List<KnowledgePoint> findAll();

    @Select("SELECT kp.*, c.category_name as categoryName " +
            "FROM knowledge_point kp " +
            "LEFT JOIN category c ON kp.category_id = c.id " +
            "WHERE kp.category_id = #{categoryId}")
    List<KnowledgePoint> findByCategoryId(Integer categoryId);

    @Update("UPDATE knowledge_point SET name = #{name}, description = #{description}, " +
            "category_id = #{categoryId} WHERE id = #{id}")
    void update(KnowledgePoint knowledgePoint);

    @Delete("DELETE FROM knowledge_point WHERE id = #{id}")
    void delete(Integer id);

    @Insert("INSERT INTO homework_knowledge (homework_id, knowledge_id) VALUES (#{homeworkId}, #{knowledgeId})")
    void addHomeworkKnowledge(@Param("homeworkId") Integer homeworkId, @Param("knowledgeId") Integer knowledgeId);

    @Delete("DELETE FROM homework_knowledge WHERE homework_id = #{homeworkId}")
    void deleteHomeworkKnowledge(Integer homeworkId);

    @Select("SELECT kp.*, c.category_name as categoryName, hq.id as questionId " +
            "FROM knowledge_point kp " +
            "LEFT JOIN category c ON kp.category_id = c.id " +
            "INNER JOIN homework_question hq ON kp.id = hq.knowledge_point_id " +
            "WHERE hq.homework_id = #{homeworkId}")
    List<KnowledgePoint> findByHomeworkId(Integer homeworkId);
} 